<?php 
class DatetimeUtil {

  public static function getTimeZomeByLocation($long, $lat) {
    try {
      if (empty($long) || empty($lat)) {
        return sfConfig::get('sf_default_timezone');
      }
      
      $geocode=file_get_contents("https://maps.googleapis.com/maps/api/timezone/json?location=$lat,$long&timestamp=".time()."&sensor=false");
      $output= json_decode($geocode);
      
      return $output->timeZoneId;
      
    } catch (Exception $e) {
      return sfConfig::get('sf_default_timezone');
    }
  }
  
  public static function convertDateTZ($timestamp, $toTZ, $fromTZ, $format = Constants::DATE_TIME_MYSQL_DB) {
    date_default_timezone_set($fromTZ);
    
    if (is_numeric($timestamp)) {
      $timestamp = gmdate(Constants::DATE_TIME_MYSQL_DB, $timestamp);
    } else {
      $timestamp = gmdate(Constants::DATE_TIME_MYSQL_DB, strtotime($timestamp));
    }
    
    $dt = new DateTime($timestamp, new DateTimeZone('UTC'));
    $dt->setTimezone(new DateTimeZone($toTZ));
    date_default_timezone_set(sfConfig::get('sf_default_timezone'));

    return $dt->format($format);
  }
  
  public static function getYearList() {
    $arrYear = array(); 
    $i = date('Y') - 20;
    for ($i; $i <= date('Y'); $i++) {
      $arrYear[$i] = $i;
    }
    
    return $arrYear;
  }
    
}
?>